package com.situ.vivamarket.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.situ.vivamarket.model.Member;
import com.situ.vivamarket.model.front.MemberAddress;
import com.situ.vivamarket.model.Order;
import com.situ.vivamarket.service.front.MemberAddrService;
import com.situ.vivamarket.service.MemberService;
import com.situ.vivamarket.service.OrderService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/vm/front/shop")
public class ShopController {
    OrderService orderService;
    AlipayClient alipayClient;
    MemberService memberService;
    MemberAddrService memberAddrService;
//    支付宝支付
    @Value("${alipay.notify-url}")
    private String notifyUrl;

    @Value("${alipay.return-url}")
    private String returnUrl;
//  依赖注入
    @Autowired
    public void setAlipayClient(AlipayClient alipayClient) {
        this.alipayClient = alipayClient;
    }
    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }
    @Autowired
    public void setMemberService(MemberService memberService) {this.memberService = memberService;}

    @Autowired
    public void setMemberAddrService(MemberAddrService memberAddrService) {
        this.memberAddrService = memberAddrService;
    }

    //         跳转购物车页面
    @GetMapping("/cart")
    public String shopCart() {
        //        把所有的购物车项取出来,Front拦截器中实现
        return "front/shop/cart";
    }

    //      跳转结算页面
    @GetMapping("/checkout")
    public String shopCheckout(Integer id, Map<String, Object> map, HttpSession session) {
        Order order = this.orderService.findById(id);
        map.put("order", order);
//        获取MemberAddress
        Member member = (Member) session.getAttribute("current_member_login");
        List<MemberAddress> mas=memberAddrService.findByMemberId(member.getId());//获取会员的地址

        map.put("mas", mas);
        return "front/shop/checkout";
    }

    //    发起支付宝支付
    @GetMapping(value = "/alipay",produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody
    private String alipay(Integer id) throws AlipayApiException {
        Order order = this.orderService.findById(id);
        Member member = this.memberService.findById(order.getMemberId());

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);

        JSONObject bizContent = new JSONObject();
        //外部的订单号
        bizContent.put("out_trade_no", order.getOrderNo());
        bizContent.put("total_amount", order.getTotalPay());
        bizContent.put("subject", order.getOrderNo() + " " + member.getAccount());
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        request.setBizContent(bizContent.toString());

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            return response.getBody();//是一个html片段
        } else {
            throw new RuntimeException("创建订单失败");
        }
    }
//    跳转到收藏商品列表
    @GetMapping("/wishlist")
    public String shopWishlist() {
//        把所有购物车项取出，在拦截器里实现
        return "front/shop/wishlist";
    }


}
